package com.bestsoft.flow.service.impl;

import com.bestsoft.baseinfo.domain.BaseInfoClient;
import com.bestsoft.baseinfo.domain.BaseInfoGoods;
import com.bestsoft.baseinfo.domain.BaseInfoSupply;
import com.bestsoft.flow.domain.FlowGyslxLs;
import com.bestsoft.flow.domain.FlowGyslxPf;
import com.bestsoft.flow.domain.FlowKcjc;
import com.bestsoft.flow.domain.Htxsml;
import com.bestsoft.flow.mapper.FlowDataTransMapper;
import com.bestsoft.flow.service.FlowDataTransService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
public class FlowDataTransServiceImpl implements FlowDataTransService {

    @Autowired
    private FlowDataTransMapper flowDataTransMapper;

    /**
     * 商品资料同步1
     *
     * @param baseInfoGoods
     */
    @Override
    public String BaseInfoGoodsTransList(BaseInfoGoods baseInfoGoods){
        List<BaseInfoGoods> baseInfoGoodsList = flowDataTransMapper.getGoodsSourceData(baseInfoGoods);
        if(baseInfoGoodsList.size()==0){
            String st1 = "无需要同步数据";
            return st1;
        }
        Long t1 = System.currentTimeMillis();
        int s = flowDataTransMapper.setGoodsTargetData(baseInfoGoodsList);
        Long t2 = System.currentTimeMillis();
        System.out.println("耗时"+(t2-t1)+"ms");
        String st = "耗时"+(t2-t1)+"ms";
        return st;
    }
    /**
     * 客户资料同步
     *
     * @param baseInfoClient
     */
    @Override
    public String BaseInfoClientTransList(BaseInfoClient baseInfoClient) {
        List<BaseInfoClient> baseInfoClientList = flowDataTransMapper.getClientSourceData(baseInfoClient);
        if(baseInfoClientList.size()==0){
            String st1 = "无需要同步数据";
            return st1;
        }

        //删除历史客户
        int i = flowDataTransMapper.delClientTargetData();

        Long t1 = System.currentTimeMillis();
        int s = flowDataTransMapper.setClientTargetData(baseInfoClientList);
        Long t2 = System.currentTimeMillis();
        System.out.println("耗时"+(t2-t1)+"ms");
        String st = "耗时"+(t2-t1)+"ms";
        return st;
    }

    /**
     * 供应商资料同步
     *
     * @param baseInfoSupply
     */
    @Override
    public String BaseInfoSupplyTransList(BaseInfoSupply baseInfoSupply) {
        List<BaseInfoSupply> baseInfoSupplyList = flowDataTransMapper.getSupplySourceData(baseInfoSupply);
        if(baseInfoSupplyList.size()==0){
            String st1 = "无需要同步数据";
            return st1;
        }
        //删除历史库存
        int i = flowDataTransMapper.delSupplyTargetData();

        Long t1 = System.currentTimeMillis();
        int s = flowDataTransMapper.setSupplyTargetData(baseInfoSupplyList);
        Long t2 = System.currentTimeMillis();
        System.out.println("耗时"+(t2-t1)+"ms");
        String st = "耗时"+(t2-t1)+"ms";
        return st;
    }

    /**
     * 客户资料同步
     *
     * @param flowSupplyDoc
     */
//    @Override
//    public String FlowClientTransList(FlowSupplyDoc flowSupplyDoc) {
//        return null;
//    }

    /**
     * 供应商流向同步(批发)
     *
     * @param flowGyslxPf
     */
    @Override
    public String flowGyslxPfTransList(FlowGyslxPf flowGyslxPf) {
        List<FlowGyslxPf> flowGyslxPfList = flowDataTransMapper.getGyslxPfSourceData(flowGyslxPf);
        if(flowGyslxPfList.size()==0){
            String st1 = "无需要同步数据";
            return st1;
        }
        Long t1 = System.currentTimeMillis();
        int s = flowDataTransMapper.setGyslxPfTargetData(flowGyslxPfList);
        Long t2 = System.currentTimeMillis();
        System.out.println("耗时"+(t2-t1)+"ms");
        String st = "耗时"+(t2-t1)+"ms";
        return st;
    }

    /**
     * 供应商流向同步(连锁)
     *
     * @param flowGyslxLs
     */
    @Override
    public String flowGyslxLsTransList(FlowGyslxLs flowGyslxLs) {
        List<FlowGyslxLs> flowGyslxLsList = flowDataTransMapper.getGyslxLsSourceData(flowGyslxLs);
        if(flowGyslxLsList.size()==0){
            String st1 = "无需要同步数据";
            return st1;
        }
        Long t1 = System.currentTimeMillis();
        int s = flowDataTransMapper.setGyslxLsTargetData(flowGyslxLsList);
        Long t2 = System.currentTimeMillis();
        System.out.println("耗时"+(t2-t1)+"ms");
        String st = "耗时"+(t2-t1)+"ms";
        return st;
    }

    /**
     * 商品库存同步
     *
     * @param flowKcjc
     */
    @Override
//    @Transactional(rollbackFor = Exception.class)
    public String flowBalanceTransList(FlowKcjc flowKcjc) {
        List<FlowKcjc> flowKcjcLsList = flowDataTransMapper.getBalanceSourceData(flowKcjc);
        if(flowKcjcLsList.size()==0){
            String st1 = "无需要同步数据";
            return st1;
        }
        //删除历史库存
        int i = flowDataTransMapper.delBalanceTargetData();
        //插入新的库存
        Long t1 = System.currentTimeMillis();
        int s = flowDataTransMapper.setBalanceTargetData(flowKcjcLsList);
        Long t2 = System.currentTimeMillis();
        System.out.println("耗时"+(t2-t1)+"ms");
        String st = "耗时"+(t2-t1)+"ms";
        return st;
    }

    /**
     * @param htxsml
     * @description: 恒泰销售毛利
     * @author cuijian
     * @date 2023/10/9 14:38
     * @version 1.0
     */
    @Override
    public String htxsmlTransList(Htxsml htxsml) {
//        String dateString = "2022-04-15";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String nextDayString = "2022-05-08";
        Date date = null;
        while( nextDayString.compareTo("2022-07-15") < 0){
            try {
                date = sdf.parse(nextDayString);
            } catch (ParseException e) {
                throw new RuntimeException(e);
            }
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            calendar.add(Calendar.DAY_OF_YEAR, 1);
            Date nextDay = calendar.getTime();
            nextDayString = sdf.format(nextDay);
            System.out.println(nextDayString);
            Long t3 = System.currentTimeMillis();
            List<Htxsml> fhtxsmlList = flowDataTransMapper.gethxsmlSourceData(nextDayString);
            Long t4 = System.currentTimeMillis();
            if(fhtxsmlList.size()==0){
                continue;
            }
            //插入新的库存
            Long t1 = System.currentTimeMillis();
            int s = flowDataTransMapper.setthxsmlTargetData(fhtxsmlList);
            Long t2 = System.currentTimeMillis();

            System.out.println("查询耗时"+(t4-t3)+"ms");
            System.out.println("插入耗时"+(t2-t1)+"ms");

        }
        String st = "总耗时";
        return st;
    }
}
